/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.activity.abstraction;

import cn.edu.thu.log.tree.ManyTreeNode;
import cn.edu.thu.log.tree.XMLNode;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.InputSource;
import storage.dbhelper.ActivityAbstractHelper;
import storage.dbobject.DbActivityAbstractRule;

/**
 * get config message from activity tag file
 * 
 * @author wan
 */
public class FromActivityTagXMLToTree {
	private Element childelement;
	private String minValue;
	private String maxValue;
/**
 * 
 * @param filename parameter
 * @return
 */
	public Element readActivityXML(String filename) {
		// parse the XML as a W3C Document
		// 使用SAXBuilder对象
		// System.out.println("readXML activity tag config filename is:" +
		// filename);
		String activityTagXMLString = "";
		ActivityAbstractHelper noiseHelper = new ActivityAbstractHelper();
		List<DbActivityAbstractRule> activityAbstractRules = noiseHelper
				.getActivityAbstractRules(DbActivityAbstractRule.CL_NAME,
						filename);
		DbActivityAbstractRule activityAbstractRule;

		if (activityAbstractRules.size() > 0) {
			activityAbstractRule = activityAbstractRules.get(0);
		}

		SAXBuilder sax = new SAXBuilder();
		StringReader sr = new StringReader(activityTagXMLString);
		InputSource source = new InputSource(sr);

		// 到数据库读取filename的xml文件
		Document doc = null;
		Element root = null;
		try {
			// doc = sax.build("E:\\treerule4.xml");
			doc = sax.build(source);
			Element xmlroot = doc.getRootElement(); // 得到根节点


			Element treeroot = xmlroot.getChild("div");
			if (treeroot.getAttributeValue("class").equals("dtree")) {
				// System.out.println("get tree root file!");
				System.out.println("test");
			}

			root = (Element) XPath.selectSingleNode(treeroot,
					"./div[@id]/div[@class=\"dTreeNode\"]");

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return root;
	}
	/**
	 * 
	 * @param node parameter
	 * @return
	 * @throws JDOMException 异常处理
	 */

	public ManyTreeNode createTree(Element node) throws JDOMException {
		int flag = 0;
		Element p = (Element) XPath.selectSingleNode(node,
				"./a[@class=\"node\"]");
		String nodename = p.getText();
		// System.out.println("treenode name!");
		String type = p.getAttributeValue("type");
		String id = p.getAttributeValue("id").substring(2);
		XMLNode xmlnode = new XMLNode(id, nodename, type);

		ArrayList<ManyTreeNode> childList = new ArrayList<ManyTreeNode>();
		ManyTreeNode rootNode = new ManyTreeNode(xmlnode, childList);

		// 创建子节点列表
		List neighbourWithChild = node.getParentElement().getChildren();
		String currentid = p.getAttributeValue("id").substring(2);
		Element e = null;
		for (int i = 0; i < neighbourWithChild.size(); i++) {
			e = (Element) neighbourWithChild.get(i);
			// 找到它的孩子节点集e
			if (e.getAttributeValue("id") != null
					&& e.getAttributeValue("id").substring(2).equals(currentid)) {
				childelement = e;

				List childNodeList = childelement.getChildren();
				if (childNodeList.isEmpty()) {
					// System.out.println("child size is:" +
					// childNodeList.size());
					return rootNode;
				} else {
					Element c = null;
					for (int j = 0; j < childNodeList.size(); j++) {
						c = (Element) childNodeList.get(j);
						if (c.getAttributeValue("class").equals("dTreeNode")) {
							childList.add(createTree(c));
						}
					}
					rootNode.setChildList(childList);
				}
			}
		}
		return rootNode;
	}
}
